// pages/start/start.js
import {
    ajaxPost,
    showToast,
}
from "../../utils/util.js";
import IMController from '../../IMJS/im'
let app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        canIUse: wx.canIUse("button.open-type.getUserInfo"),
        show: true,
        select: "",
        shareId: "",
        code: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('share-options', options);
        let that = this
        this.setData({
            select: options.identity,
            type: options.type,
            shareId: options.shareId,
        })
        let token = wx.getStorageSync("token");
        if (!token) {
            return;
        }
        wx.getSetting({
            success: res => {
                if (res.authSetting["scope.userInfo"]) {
                    let data = {
                        token: token
                    };
                    ajaxPost("/authuser/myInfo", data, res => {
                        if (res.code == 0) {
                            let userInfo = res.data.user_info
                            if (userInfo.has_mobile == 0) {
                                showToast('text', '请继续授权,获取手机号');
                                that.setData({
                                    show: false
                                })
                                return;
                            }
                            that.loginNIM()
                            if (userInfo.role == 2 && that.data.select == 'student') {
                                if (that.data.type == 'work') {
                                    wx.redirectTo({
                                        url: `/pages/student/workCont/workCont?workId=${that.data.shareId}&type=share`
                                    });
                                    return;
                                }
                                if (that.data.type == 'company') {
                                    wx.redirectTo({
                                        url: `/pages/student/companyCont/companyCont?companyId=${that.data.shareId}&type=share`
                                    });
                                    return;
                                }
                            } else {
                                that.setUserType()
                            }
                            if (userInfo.role == 1 && that.data.select == 'enterprise') {
                                if (userInfo.is_set_userinfo == 0) {
                                    wx.redirectTo({
                                        url: `/pages/enterprise/binding/binding?shareId=${that.data.shareId}&type=${that.data.type}`
                                    });
                                    return;
                                }
                                if (that.data.type == 'code') {
                                    wx.redirectTo({
                                        url: `/pages/enterprise/listCont/listCont?id=${that.data.shareId}&type=code`
                                    });
                                    return;
                                }
                                if (that.data.type == 'resume') {
                                    wx.redirectTo({
                                        url: `/pages/enterprise/listCont/listCont?id=${that.data.shareId}&type=resume`
                                    });
                                    return;
                                }
                            } else {
                                that.setUserType()
                            }
                        } else {
                            showToast('error', res.msg, {
                                duration: 3000
                            })
                        }
                    });
                }
            }
        });
    },
    /**
     * 获取授权个人信息 ----> 判断是否绑定，如果没有绑定-->绑定手机号
     *               ----> 否则去网易云登录
     */

    getUserInfo(e) {
        if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
            showToast('error', '拒绝授权将会无法使用树枝校招,请重新操作', {
                duration: 3000
            })
            return;
        }
        this.userLogin(e.detail.encryptedData, e.detail.iv)

    },
    userLogin(encryptedData, iv) {
        let that = this
        wx.login({
            success(res) {
                let data = {
                    code: res.code, // code  必须给
                    encryptedData: encryptedData, //密文串  必须给
                    iv: iv //加密初始量 必给
                };
                ajaxPost("/login/wxlogin", data, res => {
                    if (res.code == 0) {
                        wx.setStorageSync('token', res.data.token);
                        if (res.data.has_mobile == 0) {
                            showToast('text', '请继续授权,获取手机号');
                            that.setData({
                                show: false
                            })
                            return;
                        }
                        that.loginNIM();
                    } else {
                        showToast('text', res.msg)
                    }
                });

            }
        });
    },
    /**
     * 获取手机号 ---> 网易云登录
     */
    getPhoneNumber(e) {
        if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
            showToast('error', '拒绝授权将会无法使用树枝校招,请重新操作', {
                duration: 3000
            })
            return;
        }
        if (!e.detail.encryptedData) return;
        let that = this
        wx.checkSession({
            success() {
                that.loginPhone(e.detail.encryptedData, e.detail.iv)
            },
        })
    },

    /**
     * 获取手机号回调
     */

    loginPhone(encryptedData, iv) {
        let that = this
        wx.login({
            success: res => {
                let data = {
                    token: wx.getStorageSync("token"),
                    code: res.code,
                    encryptedData: encryptedData,
                    iv: iv
                };
                ajaxPost("/login/bindmobile", data, res => {
                    if (res.code == 0) {
                        that.loginNIM()
                    } else {
                        showToast('text', res.msg)
                    }
                });

            }
        });
    },

    /**
     * 获取网易云账号 ----> 选择角色
     */
    loginNIM() {
        let data = {
            token: wx.getStorageSync("token"),
        };
        ajaxPost("/authuser/getwytoken", data, res => {
            if (res.code == 0) {
                app.globalData.wytoken = res.data.wytoken
                app.globalData.account = res.data.accid
                new IMController({
                    token: res.data.wytoken,
                    account: res.data.accid,
                })
                this.setUserType()
            } else {
                showToast('text', res.msg)
            }
        });
    },
    /**
     * 选择角色   ---- > 进入页面
     */
    setUserType() {
        let data = {
            token: wx.getStorageSync("token"),
            usertype: this.data.select == "enterprise" ? 4 : 3 //学生3 企业4
        };
        ajaxPost("/authuser/usertype", data, res => {
            if (res.code == 0) {
                if (this.data.select == "student") {
                    if (this.data.type == 'work') {
                        wx.redirectTo({
                            url: `/pages/student/workCont/workCont?workId=${this.data.shareId}&type=share`
                        });
                        return;
                    }
                    if (this.data.type == 'company') {
                        wx.redirectTo({
                            url: `/pages/student/companyCont/companyCont?companyId=${this.data.shareId}&type=share`
                        });
                        return;
                    }
                }
                if (this.data.select == "enterprise") {
                    if (res.data.is_set_userinfo == 0) {
                        wx.redirectTo({
                            url: `/pages/enterprise/binding/binding?shareId=${this.data.shareId}&type=${this.data.type}`
                        });
                        return;
                    }
                    if (this.data.type == 'code') {
                        wx.redirectTo({
                            url: `/pages/enterprise/listCont/listCont?id=${this.data.shareId}&type=code`
                        });
                        return;
                    }
                    if (this.data.type == 'resume') {
                        wx.redirectTo({
                            url: `/pages/enterprise/listCont/listCont?id=${this.data.shareId}&type=resume`
                        });
                        return;
                    }
                }
            } else {
                showToast('text', res.msg)
            }
        });
    },
});